package com.gxa.controller.receipt;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.R;
import com.gxa.common.utils.Result;
import com.gxa.entity.points.Gift;
import com.gxa.entity.receipt.Bill;
import com.gxa.service.receipt.BillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:43:53
 */
@Api(tags = "发票管理模块")
@RestController
@RequestMapping("/bill")
public class BillController {

    @Autowired
    private BillService billService;

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    class K {
        String name;
        Integer count;
        Integer pageNumber;
    }

    @ApiOperation(value = "查看发票记录接口")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @GetMapping("/bills")
    public Result queryAll(@RequestParam("pageNumber") Integer pageNumber,@RequestParam("count") Integer count){
        try {
            Page<Bill> page1 = PageHelper.startPage(pageNumber,count);
            List<Bill> bills = this.billService.quetAll();
            PageInfo<Bill> pageInfo = new PageInfo<Bill>(bills);
            return Result.success(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("没有数据");
        }
    }

    @ApiOperation(value = "根据抬头类型查找发票接口")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PostMapping("/billByName")
    public Result queryByName(String name,Integer count,Integer pageNumber){
//        try {
            Page<Bill> page1 = PageHelper.startPage(pageNumber,count);
            List<Bill> bills = this.billService.queryByname(name);
            PageInfo<Bill> pageInfo = new PageInfo<Bill>(bills);
            return Result.success(bills);
//        }catch (Exception e){
//            e.printStackTrace();
//            return Result.failed("没有数据");
//        }
    }
    @ApiOperation(value = "添加发票接口")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PostMapping("/add")
    public Result add(@RequestBody Bill bill){
        try {
            this.billService.add(bill);
            return Result.success("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("添加失败");
        }
    }

    @ApiOperation(value = "修改发票")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PutMapping("/edit")
    public Result edit(@RequestBody Bill bill){
        try {
            this.billService.update(bill);
            return Result.success("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("更新失败");
        }
    }

    @ApiOperation(value = "根据id删除发票接口")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @DeleteMapping ("/delete/{id}")
    public Result delete(@PathVariable(value = "id") Integer id) {
        try {
            this.billService.delete(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("删除失败");
        }
    }

}
